package com.cmcc.groupcontacts.firewall.adapter;

import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.cmcc.groupcontacts.firewall.dao.model.SmsListVO;
import com.cmcc.groupcontacts.util.DateUtil;
import com.cmcc.groupcontacts.util.ValidationUtil;
import com.yinhai.activity.R;

/**
 * 
 * 信息拦截列表Adapter
 * 
 * @author 
 * 
 */
public class SmsListAdapter extends BaseAdapter<SmsListVO> {
	/**
	 * 视图缓存
	 */
	public final class ViewCache {
		public TextView txtName;
		public TextView txtDate;
		public TextView txtBody;
	}

	private ViewCache viewCache = null;

	public SmsListAdapter(Context context, ArrayList<SmsListVO> smsList) {
		super(R.layout.sms_list_item, context, smsList);
	}

	@Override
	protected void setViewCache(View convertView, int position) {
		viewCache = new ViewCache();
		viewCache.txtName = (TextView) convertView.findViewById(R.id.txt_name);
		viewCache.txtDate = (TextView) convertView.findViewById(R.id.txt_date);
		viewCache.txtBody = (TextView) convertView.findViewById(R.id.txt_body);
		convertView.setTag(viewCache);
	}

	@Override
	protected void getViewCache(View convertView, int position) {
		viewCache = (ViewCache) convertView.getTag();
	}

	@Override
	protected void setValueByViewCache(View convertView, int position) {
		final SmsListVO sms = getItem(position);
		// TODO: [优化]更改为显示归属地，将电话号码显示到联系人姓名后面（如果有的话）
		if (!ValidationUtil.isNullOrEmpty(sms.getName())) {
			viewCache.txtName.setText(sms.getName());
		} else {
			viewCache.txtName.setText(sms.getNumber());
		}
		viewCache.txtDate.setText(DateUtil.format2BeforeDatetime(new Date(sms.getDate())));
		viewCache.txtBody.setText(sms.getBody());
	}
}
